package cn.edu.pku.cs08.liudun.picventure.activity;

import cn.edu.pku.cs08.liudun.picventure.AppConfig;
import cn.edu.pku.cs08.liudun.picventure.R;
import cn.edu.pku.cs08.liudun.picventure.dialog.SetPaintWidthDialog;
import cn.edu.pku.cs08.liudun.picventure.view.MaskDispalyAreaView;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MaskActivity extends Activity implements OnClickListener{
	private static final String TAG = "MaskActivity";
	
	SetPaintWidthDialog setPaintWidthDialog = null;
	MaskDispalyAreaView maskDisplayAreaView = null;
	
	TextView textViewProtect = null;
	TextView textViewRemove = null;
	TextView textViewErase = null;
	TextView textViewDone = null;
	TextView textViewCancel = null;
	TextView textViewPaintWidth = null;
	
	AppConfig appConfig = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.mask);		
		
		maskDisplayAreaView = (MaskDispalyAreaView) this.findViewById(R.id.maskDispalyAreaView);
		textViewProtect = (TextView) this.findViewById(R.id.textViewProtect);
		textViewRemove = (TextView) this.findViewById(R.id.textViewRemove);
		textViewErase = (TextView) this.findViewById(R.id.textViewErase);
		textViewDone = (TextView) this.findViewById(R.id.textViewDone);
		textViewCancel = (TextView) this.findViewById(R.id.textViewCancel);
		textViewPaintWidth = (TextView) this.findViewById(R.id.textViewPaintWidth);
		
		textViewProtect.setOnClickListener(this);
		textViewRemove.setOnClickListener(this);
		textViewErase.setOnClickListener(this);
		textViewDone.setOnClickListener(this);
		textViewCancel.setOnClickListener(this);
		textViewPaintWidth.setOnClickListener(this);
		
		appConfig = (AppConfig) this.getApplicationContext();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()) {
		case R.id.textViewDone:
			onDoneMask();
			break;
		case R.id.textViewPaintWidth:
			showSetPaintWidthDialog();
			break;
		case R.id.textViewProtect:
			maskDisplayAreaView.setDrawPaintAsHighlight();
			updateColorOnClick(R.id.textViewProtect);
			break;
		case R.id.textViewRemove:
			maskDisplayAreaView.setDrawPaintAsDim();
			updateColorOnClick(R.id.textViewRemove);
			break;
		case R.id.textViewErase:
			maskDisplayAreaView.setDrawPaintAsErase();
			updateColorOnClick(R.id.textViewErase);
			break;
		case R.id.textViewCancel:
			this.finish();
			break;
		
		}		
	}
	
	private void updateColorOnClick(int id) {
		textViewRemove.setTextColor(Color.WHITE);
		textViewProtect.setTextColor(Color.WHITE);
		textViewErase.setTextColor(Color.WHITE);
		switch(id) {
		case R.id.textViewProtect:
			textViewProtect.setTextColor(Color.GREEN); //do not use R.color, or textView will disappear
			break;
		case R.id.textViewRemove:
			textViewRemove.setTextColor(Color.RED);
			break;
		}
	}
	
	private void onDoneMask() {
		appConfig.setMaskBmp(maskDisplayAreaView.maskBmp);
		this.finish();
	}

	private void showSetPaintWidthDialog(){
		if(setPaintWidthDialog == null) {
			setPaintWidthDialog = new SetPaintWidthDialog(this);
		}
		setPaintWidthDialog.setOnChangePaintWidthListener(new OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				setPaintWidthDialog.updateTextViewPaintWidth();
				appConfig.setPaintWidth(seekBar.getProgress());
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
		});
		setPaintWidthDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface arg0) {
				// TODO Auto-generated method stub
				Log.e(TAG, "cancel setPaintWidthDialog");
				maskDisplayAreaView.setPaintWidth(appConfig.getPaintWidth());
			}
			
		});
		setPaintWidthDialog.show();
	}
}
